package com.aier.entrance.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.entrance.common.EntranceICFinalData;
import com.aier.entrance.common.EntranceServerData;
import com.aier.school.entity.School;
import com.aier.school.entity.mapper.SchoolRowMapper;

public class EntranceDeviceRowMapper implements RowMapper<EntranceDevice> {

    private int type;
    
    public static final int AND_SCHOOL_AND_VIDEO=1;

    public static final int AND_SCHOOL_NAME=2;
    
    public EntranceDeviceRowMapper() {
    }

    public EntranceDeviceRowMapper(int type) {
        this.type = type;
    }

    @Override
    public EntranceDevice mapRow(ResultSet rs, int rowNum) throws SQLException {
        EntranceDevice ed=null;
        switch(type){
            case AND_SCHOOL_AND_VIDEO:
                ed=parseEntrance(new EntranceDevice(), rs,"ed");
                ed.setS(SchoolRowMapper.parseSchool(new School(), rs,"s"));
                ed.setVd(VideoDeviceRowMapper.parseVideoDevice(new VideoDevice(), rs, "vd"));
                return ed;
            case AND_SCHOOL_NAME:
                ed=parseEntrance(new EntranceDevice(), rs,"ed");
                ed.setVideo_sn(rs.getString("video_sn"));
                ed.setSchool_name(rs.getString("school_name"));
                if(ed.getState()==EntranceICFinalData.ENTRANCE_STATE_ON){
                    ed.setState_str("启用");
                }else if(ed.getState()==EntranceICFinalData.ENTRANCE_STATE_OFF){
                    ed.setState_str("停用");
                }else{
                    ed.setState_str("报废");
                }
                ed.setSupplier_str(EntranceServerData.entranceSupplier.get(ed.getSupplier()));
    
                switch(ed.getVdstate()){
                    case EntranceICFinalData.ENTRANCE_VDSTATE_ALL:
                        ed.setVdstate_str("全部拍照");
                        break;
                    case EntranceICFinalData.ENTRANCE_VDSTATE_OFF:
                        ed.setVdstate_str("不拍照");
                        break;
                    case EntranceICFinalData.ENTRANCE_VDSTATE_IN:
                        ed.setVdstate_str("进门拍照");
                        break;
                    case EntranceICFinalData.ENTRANCE_VDSTATE_OUT:
                        ed.setVdstate_str("出门拍照");
                        break;
                }
                return ed;
            default:
                return parseEntrance(new EntranceDevice(), rs);            
        }
    }

    public static EntranceDevice parseEntrance(EntranceDevice ed, ResultSet rs) throws SQLException {
        return parseEntrance(ed, rs, null);
    }

    public static EntranceDevice parseEntrance(EntranceDevice ed, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            ed.setEid(rs.getLong(su+"eid"));
        } catch (SQLException e) {
            ed.setEid(null);
        }

        try {
            ed.setSn(rs.getString(su+"sn"));
        } catch (SQLException e) {
            ed.setSn(null);
        }
        try {
            ed.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            ed.setState(null);
        }
        try {
            ed.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            ed.setSid(null);
        }
    
        try{
            ed.setDes(rs.getString(su+"des"));
        }catch (SQLException e) {
            ed.setDes(null);
        }
        
        try{
            ed.setVdid(rs.getLong(su+"vdid"));
        }catch (SQLException e) {
            ed.setVdid(null);
        }
        
        try{
            ed.setVdstate(rs.getShort(su+"vdstate"));
        }catch (SQLException e) {
            ed.setVdstate(null);
        }

        try{
            ed.setSupplier(rs.getShort(su+"supplier"));
        }catch(SQLException e){
            ed.setSupplier(null);
        }

        try{
            ed.setPid(rs.getInt(su+"pid"));
        }catch(SQLException e){
            ed.setPid(null);
        }
        try{
            ed.setPoxystate(rs.getShort(su+"poxystate"));
        }catch(SQLException e){
            ed.setPoxystate(null);
        }
        return ed;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, EntranceDevice ed) {
        par.put("eid",ed.getEid());
        par.put("sn",ed.getSn());
        par.put("state", ed.getState());
        par.put("sid", ed.getSid() );
        par.put("des",ed.getDes());
        par.put("vdid",ed.getVdid());
        par.put("vdstate",ed.getVdstate());
        par.put("supplier", ed.getSupplier());
        par.put("pid", ed.getPid());
        par.put("poxystate", ed.getPoxystate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, EntranceDevice ed) {
        args.put("eid",ed.getEid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final EntranceDevice ed) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(ed),
                        new String[] { "eid" });
                parsePS(ps, ed);
                return ps;
            }

        };
    }

    private static String parseSQL(EntranceDevice ed) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into entrance_device(");
        int count = 0;
        if (null != ed.getSn()) {
            sql.append("sn,");
            count++;
        }

        if (null != ed.getState()) {
            sql.append("state,");
            count++;
        }

        if (null != ed.getSid()) {
            sql.append("sid,");
            count++;
        }
        
        if(null!=ed.getDes()){
            sql.append("des,");
            count++;
        }
        
        if(null!=ed.getVdid()){
            sql.append("vdid,");
            count++;
        }
        
        if(null!=ed.getVdstate()){
            sql.append("vdstate,");
            count++;
        }
        
        if(null!=ed.getSupplier()){
            sql.append("supplier,");
            count++;
        }
        if(null!=ed.getPid()){
            sql.append("pid,");
            count++;
        }
        if(null!=ed.getPoxystate()){
            sql.append("poxystate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, EntranceDevice ed)
        throws SQLException {
        int count = 1;
        if (null != ed.getSn()) {
            ps.setString(count++,ed.getSn());
        }
        
        if (null != ed.getState()) {
            ps.setShort(count++,ed.getState());
        }

        if (null != ed.getSid()) {
            ps.setInt(count++,ed.getSid());
        }
        
        if(null!=ed.getDes()){
            ps.setString(count++,ed.getDes());
        }
        
        if(null!=ed.getVdid()){
            ps.setLong(count++,ed.getVdid());
        }
        
        if(null!=ed.getVdstate()){
            ps.setShort(count++,ed.getVdstate());
        }
        
        if(null!=ed.getSupplier()){
            ps.setShort(count++,ed.getSupplier());
        }
        if(null!=ed.getPid()){
            ps.setInt(count++,ed.getPid());
        }
        if(null!=ed.getPoxystate()){
            ps.setShort(count++,ed.getPoxystate());
        }

    }

}
